﻿using System;
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Windows;
using System.Windows.Controls;
using PurchaseSales.Manage.Common;
using PurchaseSales.Manage.Common.SqlHelper;
using PurchaseSales.Manage.Services;
using ReactiveUI;
using ReactiveUI.Xaml;

namespace PurchaseSales.Manage.ViewModels
{
    public class LoginViewModel : ReactiveObject, ILoginViewModel
    {
        public Action LoginSuccessAction { get; set; }

        public ReactiveCommand LoginCommand { get; set; }
        public ReactiveAsyncCommand PasswordChangedCommand { get; set; }
        private string _messageError;
        public string MessageError
        {
            get { return _messageError; }
            set { this.RaiseAndSetIfChanged(x => x.MessageError, ref _messageError, value); }
        }

        private string _userName;

        public string UserName
        {
            get { return _userName; }
            set
            {
                MessageError = string.Empty;
                this.RaiseAndSetIfChanged(x => x.UserName, ref _userName, value);
            }
        }

        public string Password { get; set; }

        public LoginViewModel()
        {
            UserName = "admin";
            LoginCommand = new ReactiveCommand();
            LoginCommand.Subscribe(s =>
                                {
                                    if (UserName == "admin" && string.IsNullOrEmpty(Password))
                                    {
                                        var window = s as Window;
                                        ApplicationCache.Set(FunctionKeys.Agent, UserName);
                                        var keys = "CommodityViewModel,CustomerViewModel,SupplierViewModel,PurchaseCommodityViewModel,SalesViewModel,AuthRoleViewModel".Split(',');
                                        ApplicationCache.Set(FunctionKeys.FunctionModules, keys);
                                        if (LoginSuccessAction != null)
                                            LoginSuccessAction();
                                        if (window != null)
                                            window.Close();
                                    }
                                    if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password))
                                    {
                                        MessageError = "用户名或密码不能为空";
                                        return;
                                    }

                                    var user = SqlLoginHelper.GetUserInDB(UserName, Password);
                                    if (user != null)
                                    {
                                        var window = s as Window;
                                        ApplicationCache.Set(FunctionKeys.Agent, UserName);
                                        var keys = SqlLoginHelper.GetFunctionKey(UserName, Password);
                                        ApplicationCache.Set(FunctionKeys.FunctionModules, keys);
                                        if (LoginSuccessAction != null)
                                            LoginSuccessAction();
                                        if (window != null)
                                            window.Close();
                                    }
                                    else
                                        MessageError = "用户名或密码错误";
                                });

            PasswordChangedCommand = new ReactiveAsyncCommand();
            PasswordChangedCommand.RegisterAsyncAction(a =>
                                                 {
                                                     MessageError = string.Empty;

                                                     var passwordBox = a as PasswordBox;
                                                     if (passwordBox != null)
                                                         Password = passwordBox.Password;
                                                 });


        }
    }
}
